﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlServerCe;
using System.Windows.Forms;
using Scan.PDAService;
using Scan.DAO;


namespace Scan
{
    class ArchivesPersons
    {
        private static void DeletePersons()
        {
            SQLiteHelper.ExecuteNonQuery("Delete from Persons", null);
        }

        //下载数据
        public static bool DownloadPersons()
        {
            //执行 WebService 方法
            ResponseBaseOfPerson response = GlobalCache.Instance.WebServiceIfe.GetPersons(false, "", "");

            if (response.ErrorInfo.ErrorCode == 1)
            {
                if (response.Lists != null && response.Lists.Length != 0)
                {
                    DeletePersons();
                    string sql = "insert into Persons (cPersonCode, cPersonName, cSex, cDuty, cDepCode) values (@PersonCode, @PersonName, @Sex, @Duty, @DepCode)";
                    SqlCeParameter[] parameters = new SqlCeParameter[5];
                    parameters[0] = new SqlCeParameter();
                    parameters[0].ParameterName = "@PersonCode";
                    parameters[0].SqlDbType = SqlDbType.NVarChar;
                    parameters[1] = new SqlCeParameter();
                    parameters[1].ParameterName = "@PersonName";
                    parameters[1].SqlDbType = SqlDbType.NVarChar;
                    parameters[2] = new SqlCeParameter();
                    parameters[2].ParameterName = "@Sex";
                    parameters[2].SqlDbType = SqlDbType.NVarChar;
                    parameters[3] = new SqlCeParameter();
                    parameters[3].ParameterName = "@Duty";
                    parameters[3].SqlDbType = SqlDbType.NVarChar;
                    parameters[4] = new SqlCeParameter();
                    parameters[4].ParameterName = "@DepCode";
                    parameters[4].SqlDbType = SqlDbType.NVarChar;

                    // 循环插入职员档案
                    foreach (Person Person in response.Lists)
                    {
                        //parameters[0].Value = Person.PersonCode;
                        //parameters[1].Value = Person.PersonName;
                        //parameters[2].Value = Person.Sex;
                        //parameters[3].Value = Person.Duty;
                        //parameters[4].Value = Person.DepCode;
                        SQLiteHelper.ExecuteNonQuery(sql, parameters);
                    }
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 获取人员档案
        /// </summary>
        /// <param name="CompCode">人员编码</param>
        /// <param name="CompName">人员名称</param>
        public static Person[] GetPersons(Boolean Fuzzyed, string PersonCode, string PersonName)
        {
            if (Configuration.Instance.OnLineType == OnLineType.Online)
            {   // 在线方式

                //执行 WebService 方法
                ResponseBaseOfPerson response = GlobalCache.Instance.WebServiceIfe.GetPersons(Fuzzyed, PersonCode, PersonName);
                if (response.ErrorInfo.ErrorCode == 1)
                {
                    if (response.Lists != null || response.Lists.Length != 0)
                    {
                        return response.Lists;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    MessageBox.Show(response.ErrorInfo.ErrorMsg, "获取人员档案出错！");
                    return null;
                }
            }
            else
            {   // 离线方式
                string sSQL = "select cPersonCode, cPersonName, cSex, cDuty, cDepCode from Persons";
                return null;
            }
        }
    }
}
